package com.xiaoai.generic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @Author
 * @Date 2021-09-07 22:27
 */
public class MainClass {
    
    public static void main(String[] args){
//        ArrayList<String> list = new ArrayList<>();
//
//        Generic<String> xiaoai = new Generic<>("xiaoai");
//        Generic<Integer> xiaoaii = new Generic<>(1111);
//
//        System.out.println(xiaoai.getKey());
//        System.out.println(xiaoaii.getKey());

//        List<? extends Serializable> list1 = toList("xiaoai", 1211213, true);
//        List<String> strings = toList("xiaoai", "fujinhui", "true");
//        for (Object obj : list1) {
//            System.out.println(obj + "--" + getObjType(obj));
//        }

//        Apple apple = new Apple();
//        apple.getKey("xiaoai");
//        Pear<String> pear1 = new Pear<>();
//        pear1.getKey("xiaoai");
//        Pear<Integer> pear2 = new Pear<>();
//        pear2.getKey(120);

//        List<String> strList = Arrays.asList("xiao", "ai", "fjh");
//        String one = getOneRandom(strList);
//        System.out.println(one);

        // 方法泛型和类泛型互不影响
//        Generic<Integer> generic = new Generic<>(111);
//        Integer key = generic.getKey();
//        String value = generic.getValue("xiaoai");

        // 通配符




    }



    public Object test(Object obj){
        return obj;
    }


    /**
     * 泛型方法
     * @param arr
     * @param <T>
     * @return
     */
    public static <T> List<T> toList(T... arr) {
        List<T> lists = new ArrayList<T>();
        for (T element : arr) {
            lists.add(element);
        }
        return lists;
    }
    public static <T> T getOne(T... arr) {
        return arr[new Random().nextInt(arr.length)];
    }
    public static <E> E getOneRandom(List<E> lists) {
        return lists.get(new Random().nextInt(lists.size()));
    }


    public static String getObjType(Object obj){
        if (obj instanceof Byte){
            return "Byte";
        } else if (obj instanceof Integer){
            return "Integer";
        } else if (obj instanceof Long){
            return "Long";
        } else if (obj instanceof Number) {
            return "Number";
        } else if (obj instanceof Float){
            return "Float";
        } else if (obj instanceof Double){
            return "Double";
        } else if (obj instanceof String){
            return "String";
        } else if (obj instanceof Boolean){
            return "Boolean";
        } else {
            return "NAN";
        }
    }

}
